package DMSXL._2024.BinaryTrees;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class N_levelOrder {
    public static void main(String[] args) {

    }
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> result = new ArrayList<>();
        if(root == null){
            return result;
        }
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            int n = queue.size();
            List<Integer> temp = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                Node cur = queue.poll();
                temp.add(cur.val);
                List<Node> children= cur.children;
                for (int j = 0; j < children.size(); j++) {
                    queue.offer(children.get(j));
                }
            }
            result.add(temp);
        }
        return result;
    }
}
